package com.peng.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.peng.constant.Constants;
import com.peng.mapper.TClueMapper;
import com.peng.model.TClue;
import com.peng.query.ClueQuery;
import com.peng.result.R;
import com.peng.service.TClueService;
import com.peng.util.JWTUtils;
import org.apache.poi.hssf.record.DVALRecord;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;

@RestController
public class ClueController {
    @Autowired
    private TClueService  tClueService;
    @Autowired
    private TClueMapper tClueMapper;

    @PreAuthorize(value = "hasAuthority('clue:list')")
    @GetMapping("/api/clues")
    public R cluePage(@RequestParam(value = "current", required = false) Integer current) {
        if (current == null) {current = 1;}
        IPage<TClue> tClueIPage = tClueMapper
                .selectUserPage(new Page<TClue>(current, Constants.PAGE_SIZE));
        return R.OK(tClueIPage);
    }

    @PreAuthorize(value = "hasAuthority('clue:import')")
    @PostMapping(value = "/api/importExcel")
    public R importExcel(@RequestParam(value="file") MultipartFile file, @RequestHeader(value = "Authorization") String token) throws IOException { //file的名字要和前端formData里面的名字相同，否则接收不到
        tClueService.importExcel(file.getInputStream(), token);
        return R.OK();
    }

    //获取指定 id 编号的线索信息
    @PreAuthorize(value = "hasAuthority('clue:view')")
    @GetMapping(value = "/api/clue/{id}")
    public R loadClue(@PathVariable(value = "id") Integer id) {
        TClue tClue = tClueService.getClueById(id);
        return R.OK(tClue);
    }

    @PreAuthorize(value = "hasAuthority('clue:add')")
    @Transactional(rollbackFor = Exception.class)
    @PostMapping(value = "/api/clue")
    public R addClue(@ModelAttribute ClueQuery clueQuery, @RequestHeader(value = "Authorization") String token) {
        TClue tClue = new TClue();

        //判断手机号是否重复
        LambdaQueryWrapper<TClue> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TClue::getPhone, clueQuery.getPhone());
        Long count = tClueMapper.selectCount(queryWrapper);
        if(count > 0) {
            //使用过
            throw new RuntimeException("该手机号已经录入过了，不能再录入");
        }else{
            BeanUtils.copyProperties(clueQuery, tClue);

            tClue.setCreate_time(new Date());
            tClue.setCreate_by(JWTUtils.parseUserFromJWT(token).getId());
        }
        int save = tClueMapper.insert(tClue);

        return save >= 1 ? R.OK() : R.FAIL();
    }
    @PreAuthorize(value = "hasAuthority('clue:edit')")
    @PutMapping(value = "/api/clue")
    public R editClue(@ModelAttribute ClueQuery clueQuery, @RequestHeader(value = "Authorization") String token) {
        TClue tClue = new TClue();

        BeanUtils.copyProperties(clueQuery, tClue);

        tClue.setEdit_time(new Date());
        tClue.setEdit_by(JWTUtils.parseUserFromJWT(token).getId());

        int update = tClueMapper.updateById(tClue);
        return update >= 1 ? R.OK() : R.FAIL();
    }

}
